# coding=utf-8
# 滑块移动的最大距离 238px
# 滑块移动 238px（最大距离），图像刚好旋转 360° （一圈）
# div class='passMod_slide-btn  passMod_slide-btn-loading' 按钮元素
# img class='passMod_spin-background' 旋转图片元素

from DrissionPage import Chromium
from DrissionPage.common import Actions
from config import cookies as ck
import base64

tab = Chromium().latest_tab
tab.set.cookies(ck)

tab.get(
    "https://wappass.baidu.com/static/captcha/tuxing_v2.html?ak=uvErNgFVAw2s19CYSdl6FLj1U3ACoUjB&backurl=http%3A%2F%2Faiqicha.baidu.com%2Fcompany_detail_94741586043199%3Fp_type%3D1&timestamp=1744875730&signature=c7253e380792e550d8d7cad5ad8a548d")

spin_wrapper = tab.ele('@id=spin-0')  # 定位到旋转验证码浮层
img_url = spin_wrapper.ele('@class=passMod_spin-background').attr('src')  # 背景图片
btn_slider = spin_wrapper.ele("@class=passMod_slide-btn  passMod_slide-btn-loading")  # 滑块
print(img_url)
print(btn_slider)
tab.change_mode(mode='s', go=False)
tab.get(img_url)

with open("spin-img.jpeg", 'wb') as f:
    f.write(tab.response.content)

data = {
    "token": "nukHZ6acJSEEkEG6RLP3p1ySuchLQTcH-Y-0bDNlJA8",
    "type": "900011",
    "image": base64.b64encode(tab.response.content).decode('utf-8'),
}
tab.post("http://api.jfbym.com/api/YmServer/customApi", headers={"Content-Type": "application/json"}, json=data)
print(tab.response.json())
if tab.response.json().get("msg") == "识别成功":
    data = int(tab.response.json().get("data").get("data"))
    if data < 0:
        distance = round(((360 - data) // 360) * 238)  # 四舍五入到整数
    else:
        distance = round((data / 360) * 238)
    print("移动距离", distance)

    tab.change_mode(mode='d', go=False)
    tab.actions.move_to(btn_slider).hold()
    tab.actions.right(distance)
    tab.actions.release()
